/*
 * lcmd_mysql.h
 *
 *  Created on: 09.03.2011
 *      Author: michael
 */

#ifndef LCMD_MYSQL_H_
#define LCMD_MYSQL_H_

#include "lcmd_includes.h"

class Lcmd;

class LcmdMySQL
{
public:
	LcmdMySQL(Lcmd *lcmd, string server, string user, string pass, string db, int port);

	/* Client Part */
	lcm::SVEC1D getClients();
	int addClient(string clientname);
	int removeClient(string clientname);
	bool doesClientExist(string clientname);
	int getClientID(string clientname);
	void showClients();

	/* Group Part */
	lcm::SVEC1D getGroups();
	int addGroup(string groupname, string description);
	int removeGroup(string groupname);
	bool doesGroupExist(string groupname);
	int getGroupID(string groupname);
	void showGroups();

	/* Script Part */
	lcm::SVEC1D getScripts();
	int addScript(string scriptname, string filename, string description);
	int removeScript(string scriptname);
	bool doesScriptExist(string scriptname);
	int getScriptID(string scriptname);
	string getScriptsPath(string scriptname);
	string getScriptsMD5(string scriptname);
	void showScripts();

	/* Group-Client-Assignment */
	int addClientToGroup(string client,string group);
	int removeClientFromGroup(string client, string group);
	lcm::SVEC1D getClientsGroups(string clientname);
	bool isClientInGroup(string client, string group);
	bool isClientInAGroup(string client);
	bool hasGroupClients(string group);
	lcm::SVEC1D getGroupsClients(string group);

	/* Group-Script-Assignment */
	int addScriptToGroup(string script, string group);
	int removeScriptFromGroup(string script, string group);
	bool isScriptInGroup(string scriptname, string groupname);
	bool isScriptInAGroup(string scriptname);
	lcm::SVEC1D getGroupsScripts(string groupname);
	lcm::SVEC1D getClientsScripts(string clientname);
	lcm::SVEC1D getScriptsGroups(string scriptname);

	/* Client-Script-Assignment (maybe Client-Script-Log fits better */
	int addScriptToClient(string clientname, string scriptname, string datetime, int retCode);
	void showScriptsOfClient(string client);

private:
	string server;
	string user;
	string pass;
	string db;
	int port;

	lcm::SVEC2D createSVEC2D(MYSQL_RES *result);
	int execSQL(string sqlCommand, lcm::SVEC2D &relation);
	int execSQL(string sqlCommand);
	string getTupel(int row,int field);
	lcm::SVEC2D getRelation();

	int lastError;

	Lcmd *lcmd;
};


#endif /* LCMD_MYSQL_H_ */
